from .block_reader  import *
from .models import Block_profile,Block_profile_main,Market_data_update_stat

#这里只找出新增的哪个概念板块就可以，而对原来概念板块中是否新增个股则不检查。
def updateGnBlockInfo(date_now,update_start_date,update_end_date):
    #读取概念板块文件
    blocklist_pd = gn_block('gn')
    blocklist = blocklist_pd.values.tolist()
    # print("blockList = ",blocklist)
    # dataList = []
    blockcodelist_inFile = []
    itemList = []
    itemList_main = []
    blockProfileMainObj = Block_profile_main.objects.all()
    for gnblock in blocklist:
        block_name = gnblock[0]
        block_code = gnblock[1]
        stock_num = gnblock[3]
        stock_list = gnblock[4]
        if stock_num > 0:
            for i in range(0,stock_num):
                stockcode = stock_list[i]
                # {dataList.append'block_code':block_code,'block_name':block_name,'stock_num':stock_num,'stock_code':stockcode}
                # （dataList.append([block_code,block_name,stock_num,stockcode)
                item = Block_profile(block_code = block_code,block_name = block_name,total_num = stock_num,stock_code = stockcode,block_type = 'gn')
                itemList.append(item)
            item_main = Block_profile_main(block_code = block_code,block_name = block_name,total_num = stock_num,block_type = 'gn')
            itemList_main.append(item_main)
            blockcodelist_inFile.append(block_code)
        # item_stat = Block_profile_stat(block_code = block_code,block
    blockProfileMainObj = Block_profile_main.objects.filter(block_type = 'gn').values_list('block_code', flat=True)
    blockcodelist_inDB = [ele for ele in blockProfileMainObj]
    #用来显示那些板块是新板块
    blockcodeList_new = list(set(blockcodelist_inFile).difference(set(blockcodelist_inDB)))
    blockcodeList_del = list(set(blockcodelist_inDB).difference(set(blockcodelist_inFile)))

    # print("blockcodeList_new = ",blockcodeList_new)
    for blockcode in blockcodeList_new:
        block_name_1 = [block[0] for block in blocklist if block[1] == blockcode][0]
        item = Market_data_update_stat(update_date = date_now,trade_date_start = update_start_date,
            trade_date_end = update_end_date,code = blockcode,name = block_name_1,code_type = 'block',
            event_type = 'new_block_gn')
        item.save()
    for blockcode in blockcodeList_del:
        block_name_1 = [block[0] for block in blocklist if block[1] == blockcode][0]
        item = Market_data_update_stat(update_date = date_now,trade_date_start = update_start_date,
            trade_date_end = update_end_date,code = blockcode,name = block_name_1,code_type = 'block',
            event_type = 'del_block_gn')
        item.save()

    try:
        Block_profile.objects.bulk_create(itemList)
        #直接删除原来的全部概念板块信息
        Block_profile_main.objects.filter(block_type = 'gn').delete()
        #重新写入
        Block_profile_main.objects.bulk_create(itemList_main)
    except Exception as e:
        print("Block_profile gn save exception = ",e)


def updateHyBlockInfo(date_now,update_start_date,update_end_date):
    #读取概念板块文件
    blocklist_pd = hy_block('hy')
    blocklist = blocklist_pd.values.tolist()
    # print('hy blocklist = ',blocklist)
    itemList = []
    blockInfoList = []
    hyblockList = []
    itemList_main = []
    blockcodelist_inFile = []
    for hyblock in blocklist:
        block_name = hyblock[0]
        block_code = hyblock[1]
        block = hyblock[2] #例如T110203。如果为1级行业的block5则为T1102，如果其有二级行业则从T110201开始作为block的值
        hyblockList.append(block)
        block5 = hyblock[3]#例如T1102
        stock_num = hyblock[4]
        blockInfoList.append({'name':block_name,'code':block_code,'block':block,'block5':block5,'num':stock_num})

        if block == block5:
            block_type = 'hy1'
        elif block[:5] == block5:
            block_type = 'hy2'#二级行业
        else:
            block_type = "hyx"
        
        stock_list = eval(hyblock[5])#eval 功能强大，将“[,,,,,,]”中的列表直接转换成列表
        if stock_num > 0:
            for i in range(0,stock_num):
                stockcode = stock_list[i]
                # dataList.append([block_code,block_name,block,block5,stock_num,stockcode])
                item = Block_profile(block_code = block_code,block_name = block_name,total_num = stock_num,stock_code = stockcode,block_type = block_type)
                itemList.append(item)
 
    

    for blockInfo in blockInfoList:
        tmp_name = blockInfo['name']
        tmp_code = blockInfo['code']
        tmp_block = blockInfo['block']
        tmp_block5 = blockInfo['block5']
        tmp_num = blockInfo['num']

        if tmp_block == tmp_block5:#一级行业
            if tmp_block5 + '01' in hyblockList:#有二级行业
                #如果有二级行业（细分行业）则一级行业不存储。与tdx保持一致。
                continue
            item_main = Block_profile_main(block_code = tmp_code,block_name = tmp_name,total_num = tmp_num,block_type = 'hy1')
            itemList_main.append(item_main)
        else:#二级行业
            item_main = Block_profile_main(block_code = tmp_code,block_name = tmp_name,total_num = tmp_num,block_type = 'hy2')
            itemList_main.append(item_main)
        
        blockcodelist_inFile.append(tmp_code)
    
    blockProfileMainObj = Block_profile_main.objects.filter(block_type__in = ['hy1','hy2']).values_list('block_code', flat=True)
    blockcodelist_inDB = [ele for ele in blockProfileMainObj]
    #用来显示那些板块是新板块
    blockcodeList_new = list(set(blockcodelist_inFile).difference(set(blockcodelist_inDB)))
    blockcodeList_del = list(set(blockcodelist_inDB).difference(set(blockcodelist_inFile)))
    # print("blockcodeList_new_hy = ",blockcodeList_new)
    for blockcode in blockcodeList_new:
        block_name_1 = [block[0] for block in blocklist if block[1] == blockcode][0]
        item = Market_data_update_stat(update_date = date_now,trade_date_start = update_start_date,
            trade_date_end = update_end_date,code = blockcode,name = block_name_1,code_type = 'block',
            event_type = 'new_block_hy')
        item.save()
    for blockcode in blockcodeList_del:
        block_name_1 = [block[0] for block in blocklist if block[1] == blockcode][0]
        item = Market_data_update_stat(update_date = date_now,trade_date_start = update_start_date,
            trade_date_end = update_end_date,code = blockcode,name = block_name_1,code_type = 'block',
            event_type = 'del_block_hy')
        item.save()
    try:
        Block_profile.objects.bulk_create(itemList)
    except Exception as e:
            print("Block_profile hy save exception = ",e)

    try:
        Block_profile_main.objects.filter(block_type__in = ['hy1','hy2']).delete()
        Block_profile_main.objects.bulk_create(itemList_main)
    except Exception as e:
        print("Block_profile_main hy save exception = ",e)

    # print('datalist = ',dataList)


def updateZsBlockInfo():
    #读取指数板块文件
    blocklist_pd = zs_block('zs')
    blocklist = blocklist_pd.values.tolist()
    # print("blockList = ",blocklist)
    dataList = []
    itemList = []
    for zsblock in blocklist:
        block_name = zsblock[0]
        block_code = zsblock[1]
        stock_num = zsblock[3]
        stock_list = zsblock[4]
        if stock_num > 0:
            for i in range(0,stock_num):
                stockcode = stock_list[i]
                # dataList.append([block_name,block_code,stock_num,stockcode])
                item = Block_profile(block_code = block_code,block_name = block_name,total_num = stock_num,stock_code = stockcode,block_type = 'zs')
                itemList.append(item)
    try:
        Block_profile.objects.bulk_create(itemList)
    except Exception as e:
            print("Block_profile zs save exception = ",e)
    # print('datalist = ',dataList)

def updateFgBlockInfo():
    #读取概念板块文件
    blocklist_pd = gn_block('fg')
    blocklist = blocklist_pd.values.tolist()
    # print("blockList = ",blocklist)
    # dataList = []
    itemList = []
    for fgblock in blocklist:
        block_name = fgblock[0]
        block_code = fgblock[1]
        stock_num = fgblock[3]
        stock_list = fgblock[4]
        if stock_num > 0:
            for i in range(0,stock_num):
                stockcode = stock_list[i]
                # dataList.append({'block_code':block_code,'block_name':block_name,'stock_num':stock_num,'stock_code':stockcode})
                # dataList.append([block_code,block_name,stock_num,stockcode])
                item = Block_profile(block_code = block_code,block_name = block_name,total_num = stock_num,stock_code = stockcode,block_type = 'fg')
                itemList.append(item)
    try:
        Block_profile.objects.bulk_create(itemList)
    except Exception as e:
            print("Block_profile fg save exception = ",e)
    # print('datalist = ',dataList)
